//
//  WBPhotoBroserViewController.swift
//  微博Demo
//
//  Created by 花化华 on 2016/11/29.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class WBPhotoBroserViewController: UIViewController {

    /// 让状态栏变亮
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .lightContent
    }
    
    //图片下表
    var index: Int
    //图片所在数组
    var urls: [String]
    
    //构造函数
    init (index: Int, urls: [String]) {
        self.index = index
        self.urls = urls
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //0.设置背景色
        self.view.backgroundColor = UIColor.black
        
        //1. 创建pageViewcontroller
        /// transitionStyle: 翻页的样式, .scroll代表滚动; .curl代表卷页
        /// navigationOrientation: 水平还是垂直滚动
        /// options: UIPageViewControllerOptionInterPageSpacingKey 可以设页面之间的间隔
        let pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: [UIPageViewControllerOptionInterPageSpacingKey: 20])
        
        //2. 添加pageViewController到当前controller
        self.addChildViewController(pageViewController)
        self.view.addSubview(pageViewController.view)
        pageViewController.didMove(toParentViewController: self)
        
        //3. 给pageViewCotroller设置子控制器的数组
        let photoViewer = WBPhotoViewerViewController(index: index, url: urls[index])
        let controllers = [photoViewer]
        pageViewController.setViewControllers(controllers, direction: .forward, animated: true, completion: nil)
        
        //4. 设置数据源
        pageViewController.dataSource = self
        
        //5. 设置手势
        let tapGesture =  UITapGestureRecognizer(target: self, action: #selector(tap(tap:)))
        pageViewController.view.addGestureRecognizer(tapGesture)
        self.view.gestureRecognizers = pageViewController.view.gestureRecognizers
    }
    
}


// MARK: - 点击手势
extension WBPhotoBroserViewController {
     func tap (tap: UITapGestureRecognizer) {
        dismiss(animated: false, completion: nil)
    }
}


// MARK: - pageViewController数据源
extension WBPhotoBroserViewController: UIPageViewControllerDataSource {
    /// 返回前一个子控制器
    ///
    /// - parameter pageViewController: 当前的pageViewcontroller
    /// - parameter viewController:     当前的子控制器
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        
        //获取当前控制器的下标
        let currentIndex = (viewController as! WBPhotoViewerViewController).index
        if currentIndex > 0 {
            let photoViewer = WBPhotoViewerViewController(index: currentIndex-1, url: urls[currentIndex-1])
            return photoViewer
        }
        return nil
        
    }
    
    /// 返回后一个子控制器
    ///
    /// - parameter pageViewController: 当前的pageViewcontroller
    /// - parameter viewController:     当前的子控制器
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        
        //获取当前控制器的下标
        let currentIndex = (viewController as! WBPhotoViewerViewController).index
        if currentIndex < urls.count - 1 {
            let photoViewer = WBPhotoViewerViewController(index: currentIndex+1, url: urls[currentIndex+1])
            return photoViewer
        }
        return nil

    }
}
